package com.aia.prm.web.ria.app.ui;

import com.aia.prm.BeanFactory;
import com.aia.prm.web.ria.app.PrmApplication;
import com.cfh.domain.User;
import com.vaadin.data.validator.StringLengthValidator;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.PasswordField;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

public class LoginView extends VerticalLayout {
	private static final long serialVersionUID = 1L;
	private TextField loginField;
	private PasswordField passwordField;
	private Button submitButton;
	PrmApplication app;

	public LoginView(PrmApplication aapp) {
		setSizeFull();
		this.app = aapp;
		loginField = new TextField("用户名:");
		loginField.addValidator(new StringLengthValidator("Password must be 1-20 characters", 1, 20, false));
		loginField.setRequired(true);
		loginField.setRequiredError("不能为空");

		passwordField = new PasswordField("密码:");
		passwordField.addValidator(new StringLengthValidator("Password must be 1-20 characters", 1, 20, false));
		passwordField.setRequired(true);
		passwordField.setRequiredError("不能为空");

		submitButton = new Button("确定");

		submitButton.addListener(new ClickListener() {
			public void buttonClick(ClickEvent event) {
				try {
					loginField.validate();
					passwordField.validate();
				} catch (Exception e) {
					return;
				}

				String login = (String) loginField.getValue();
				String psw = (String) passwordField.getValue();

				User user = BeanFactory.get().getDefaultService().getUserByUsernameAndPassword(login, psw);

				if (user != null) {
					LoginView.this.app.setUser(user);
					LoginView.this.app.getMainWindow().showNotification("欢迎您!" + login);
					LoginView.this.getApplication().setUser(user);
					LoginView.this.app.getMainWindow().removeAllComponents();
					G2View g2View = new G2View(LoginView.this.app);
					LoginView.this.app.getMainWindow().setContent(g2View);
				} else {
					LoginView.this.app.getMainWindow().showNotification("您输入的用户名或密码错误", Window.Notification.TYPE_WARNING_MESSAGE);
				}

			}
		});
		setCaption("Administrator console");
		FormLayout formLayout = new FormLayout();
		formLayout.setMargin(true);
		formLayout.setSpacing(true);
		formLayout.addComponent(loginField);
		formLayout.addComponent(passwordField);
		formLayout.addComponent(submitButton);
		formLayout.setComponentAlignment(submitButton, Alignment.BOTTOM_RIGHT);
		formLayout.setWidth("300px");
		Label title = new Label("<div style='text-align:center; font-size:16px;'><b>Administrator console</b></div>", Label.CONTENT_XHTML);
		title.setWidth("300px");

		setMargin(true);
		setMargin(true);
		addComponent(title);
		addComponent(formLayout);
		setExpandRatio(title, 1);
		setExpandRatio(formLayout, 1);
		setComponentAlignment(title, Alignment.BOTTOM_CENTER);
		setComponentAlignment(formLayout, Alignment.TOP_CENTER);

	}

	public void authenticate(ClickEvent event) {

	}
}